package com.haixiaoke.saas.repository.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.haixiaoke.saas.repository.mapper.UserFloatingPopulationRecordMapper;
import com.haixiaoke.saas.repository.pojo.UserFloatingPopulationRecord;
import com.haixiaoke.saas.repository.service.IUserFloatingPopulationRecordService;
import com.haixiaoke.saas.repository.util.timePair.WeeklyTimeUtil;
import com.haixiaoke.saas.repository.vo.UserFloatingPopulationRecordVo;
import com.haixiaoke.saas.repository.vo.WeeklyTimeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 流动用户记录Service业务层处理
 *
 * @author qixi
 * @date 2023-07-25
 */
@Service
public class UserFloatingPopulationRecordServiceImpl implements IUserFloatingPopulationRecordService {
    @Autowired
    private UserFloatingPopulationRecordMapper userFloatingPopulationRecordMapper;
    @Autowired
    private WeeklyTimeUtil weeklyTimeUtil;


    /**
     * 查询流动用户记录
     *
     * @param id 流动用户记录主键
     * @return 流动用户记录
     */
    @Override
    public UserFloatingPopulationRecord selectUserFloatingPopulationRecordById(Long id) {
        return userFloatingPopulationRecordMapper.selectUserFloatingPopulationRecordById(id);
    }

    /**
     * 查询流动用户记录列表
     *
     * @param userFloatingPopulationRecord 流动用户记录
     * @return 流动用户记录
     */
    @Override
    public List<UserFloatingPopulationRecord> selectUserFloatingPopulationRecordList(UserFloatingPopulationRecord userFloatingPopulationRecord) {
        return userFloatingPopulationRecordMapper.selectUserFloatingPopulationRecordList(userFloatingPopulationRecord);
    }

    /**
     * 查询流动用户记录列表
     *
     * @param userFloatingPopulationRecord 流动用户记录
     * @return 流动用户记录
     */
    @Override
    public List<UserFloatingPopulationRecordVo> selectUserFloatingPopulationRecordAndHouseInfoList(UserFloatingPopulationRecord userFloatingPopulationRecord) {
        return userFloatingPopulationRecordMapper.selectUserFloatingPopulationRecordAndHouseInfoList(userFloatingPopulationRecord);
    }

    @Override
    public List<UserFloatingPopulationRecordVo> selectUserFloatingPopulationRecordAndHouseInfoListByPopulationIds(UserFloatingPopulationRecordVo vo) {
        return userFloatingPopulationRecordMapper.selectUserFloatingPopulationRecordAndHouseInfoListByPopulationIds(vo);
    }

    /**
     * 新增流动用户记录
     *
     * @param userFloatingPopulationRecord 流动用户记录
     * @return 结果
     */
    @Override
    public int insertUserFloatingPopulationRecord(UserFloatingPopulationRecord userFloatingPopulationRecord) {
        userFloatingPopulationRecord.setCreateTime(new Date());
        return userFloatingPopulationRecordMapper.insertUserFloatingPopulationRecord(userFloatingPopulationRecord);
    }

    /**
     * 修改流动用户记录
     *
     * @param userFloatingPopulationRecord 流动用户记录
     * @return 结果
     */
    @Override
    public int updateUserFloatingPopulationRecord(UserFloatingPopulationRecord userFloatingPopulationRecord) {
        return userFloatingPopulationRecordMapper.updateUserFloatingPopulationRecord(userFloatingPopulationRecord);
    }

    /**
     * 批量删除流动用户记录
     *
     * @param ids 需要删除的流动用户记录主键
     * @return 结果
     */
    @Override
    public int deleteUserFloatingPopulationRecordByIds(Long[] ids) {
        return userFloatingPopulationRecordMapper.deleteUserFloatingPopulationRecordByIds(ids);
    }

    /**
     * 删除流动用户记录信息
     *
     * @param id 流动用户记录主键
     * @return 结果
     */
    @Override
    public int deleteUserFloatingPopulationRecordById(Long id) {
        return userFloatingPopulationRecordMapper.deleteUserFloatingPopulationRecordById(id);
    }

    /**
     * 本日人员统计
     *
     * @param orgId                组织id
     * @param residenceCommunityId 小区id
     * @param communityIds
     * @return
     */
    @Override
    public UserFloatingPopulationRecord todayPersonnel(Long orgId, String residenceCommunityId, List<String> communityIds) {
        UserFloatingPopulationRecord userFloatingPopulationRecord = new UserFloatingPopulationRecord();
        //查询当前机构下本日迁出的人员数量
        Integer emigrantsNumber = userFloatingPopulationRecordMapper.selectTotalNumber(orgId, residenceCommunityId, communityIds);
        //查询当前机构下本日新增人员数量
        Integer newPersonnelNumber = userFloatingPopulationRecordMapper.selectNewPersonnelNumber(orgId, residenceCommunityId, communityIds);

        //如果不存在小区则直接返回
        if (CollectionUtil.isEmpty(communityIds)) {
            userFloatingPopulationRecord.setNewPersonnelNumber(0);
            userFloatingPopulationRecord.setEmigrantsNumber(0);
            return userFloatingPopulationRecord;
        }
        userFloatingPopulationRecord.setNewPersonnelNumber(newPersonnelNumber);
        userFloatingPopulationRecord.setEmigrantsNumber(emigrantsNumber);
        return userFloatingPopulationRecord;
    }

    /**
     * 每周人员类型数据统计
     *
     * @param orgId                组织id
     * @param residenceCommunityId 小区id
     * @param weeklyNumber         周数
     * @param communityIds
     * @return
     */
    @Override
    public UserFloatingPopulationRecord weeklyPersonnel(Long orgId, String residenceCommunityId, Integer weeklyNumber, List<String> communityIds) {
        UserFloatingPopulationRecord userFloatingPopulationRecord = new UserFloatingPopulationRecord();
        String startDate;
        String endDate;
        //获取当前时间是本月第几周
        Integer weekNumber = weeklyTimeUtil.getWeek();

        if (weeklyNumber == null) {
            WeeklyTimeVo weeklyTime = weeklyTimeUtil.getWeeklyTime(weekNumber);
            startDate = weeklyTime.getStartDate();
            endDate = weeklyTime.getEndDate();
        } else {
            WeeklyTimeVo weeklyTime = weeklyTimeUtil.getWeeklyTime(weeklyNumber);
            startDate = weeklyTime.getStartDate();
            endDate = weeklyTime.getEndDate();
            weekNumber = weeklyNumber;
        }

        //查询出当前机构这周新增人员数量
        Integer newPersonnelNumber = userFloatingPopulationRecordMapper.selectWeeklyNewPersonnel(orgId, startDate, endDate, residenceCommunityId, communityIds);

        //查询出前端机构这周迁出的人员数量
        Integer emigrantsNumber = userFloatingPopulationRecordMapper.selectWeeklyEmigrantsPersonnel(orgId, startDate, endDate, residenceCommunityId, communityIds);
        userFloatingPopulationRecord.setWeeksNumber(weekNumber);

        //如果不存在小区则直接返回
        if (CollectionUtil.isEmpty(communityIds)) {
            userFloatingPopulationRecord.setNewPersonnelNumber(0);
            userFloatingPopulationRecord.setEmigrantsNumber(0);
            return userFloatingPopulationRecord;
        }
        userFloatingPopulationRecord.setNewPersonnelNumber(newPersonnelNumber);
        userFloatingPopulationRecord.setEmigrantsNumber(emigrantsNumber);
        return userFloatingPopulationRecord;
    }

    /**
     * 获取当前月有多少周
     */
    @Override
    public List<Map<String, Object>> getWeekNumber() {
        Integer weeklyNumber = weeklyTimeUtil.getWeeklyNumber();
        List<Map<String, Object>> weekList = new ArrayList<>();
        for (int i = 1; i <= weeklyNumber; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("value", i);
            map.put("label", "第" + i + "周");
            weekList.add(map);
        }
        return weekList;

    }

    /**
     * 月度人员数据统计
     *
     * @param orgId                组织id
     * @param residenceCommunityId 小区id
     * @param month                月份
     * @param communityIds
     * @return
     */
    @Override
    public UserFloatingPopulationRecord monthPersonnel(Long orgId, String residenceCommunityId, Integer month, List<String> communityIds) {
        UserFloatingPopulationRecord userFloatingPopulationRecord = new UserFloatingPopulationRecord();

        //获取当前时间
        LocalDate monthDate = LocalDate.now();
        Integer month1 = monthDate.getMonthValue();
        if (month != null) {
            Integer year = LocalDate.now().getYear();
            monthDate = LocalDate.of(year, month, 1);
            month1 = month;
        }

        //查询出当前机构月度新增人员数量
        Integer newPersonnelNumber = userFloatingPopulationRecordMapper.selectMonthNewPersonnel(orgId, monthDate, residenceCommunityId, communityIds);
        //查询出前端机构月度迁出的人员数量
        Integer emigrantsNumber = userFloatingPopulationRecordMapper.selectMonthEmigrantsPersonnel(orgId, monthDate, residenceCommunityId, communityIds);
        userFloatingPopulationRecord.setMonth(month1);

        //如果不存在小区则直接返回
        if (CollectionUtil.isEmpty(communityIds)) {
            userFloatingPopulationRecord.setNewPersonnelNumber(0);
            userFloatingPopulationRecord.setEmigrantsNumber(0);
            return userFloatingPopulationRecord;
        }
        userFloatingPopulationRecord.setNewPersonnelNumber(newPersonnelNumber);
        userFloatingPopulationRecord.setEmigrantsNumber(emigrantsNumber);
        return userFloatingPopulationRecord;
    }

    /**
     * 季度人员数据统计
     *
     * @param orgId                组织id
     * @param residenceCommunityId 小区id
     * @param quarter              季度
     * @param communityIds
     * @return
     */
    @Override
    public UserFloatingPopulationRecord quarterPersonnel(Long orgId, String residenceCommunityId, Integer quarter, List<String> communityIds) {
        UserFloatingPopulationRecord userFloatingPopulationRecord = new UserFloatingPopulationRecord();
        String startDate;
        String endDate;
        Integer quarter1;

        if (quarter != null) {
            quarter1 = quarter;
        } else {
            //获取当前时间
            LocalDate monthDate = LocalDate.now();
            //获取当前月份
            Integer month1 = monthDate.getMonthValue();
            if (month1 >= 1 && month1 <= 3) {
                quarter1 = 1;
            } else if (month1 >= 4 && month1 <= 6) {
                quarter1 = 2;
            } else if (month1 >= 7 && month1 <= 9) {
                quarter1 = 3;
            } else {
                quarter1 = 4;
            }
        }

        WeeklyTimeVo weeklyTime = weeklyTimeUtil.getQuarter(quarter1);
        startDate = weeklyTime.getStartDate();
        endDate = weeklyTime.getEndDate();

        //查询出当前机构季度新增人员数量
        Integer newPersonnelNumber = userFloatingPopulationRecordMapper.selectQuarterNewPersonnel(orgId, startDate, endDate, residenceCommunityId, communityIds);
        //查询出前端机构季度迁出的人员数量
        Integer emigrantsNumber = userFloatingPopulationRecordMapper.selectQuarterEmigrantsPersonnel(orgId, startDate, endDate, residenceCommunityId, communityIds);
        userFloatingPopulationRecord.setQuarter(quarter1);

        //如果不存在小区则直接返回
        if (CollectionUtil.isEmpty(communityIds)) {
            userFloatingPopulationRecord.setNewPersonnelNumber(0);
            userFloatingPopulationRecord.setEmigrantsNumber(0);
            return userFloatingPopulationRecord;
        }
        userFloatingPopulationRecord.setNewPersonnelNumber(newPersonnelNumber);
        userFloatingPopulationRecord.setEmigrantsNumber(emigrantsNumber);
        return userFloatingPopulationRecord;
    }

    /**
     * 年度人员数据统计
     *
     * @param orgId                组织id
     * @param residenceCommunityId 小区id
     * @param year                 年度
     * @param communityIds
     * @return
     */
    @Override
    public UserFloatingPopulationRecord yearPersonnel(Long orgId, String residenceCommunityId, Integer year, List<String> communityIds) {
        UserFloatingPopulationRecord userFloatingPopulationRecord = new UserFloatingPopulationRecord();

        //获取当前时间
        String strYear = DateUtil.now();
        Integer thisYear = DateUtil.thisYear();
        if (year != null) {
            strYear = year + "-01" + "-01";
            thisYear = year;
        }

        //查询出当前机构月度新增人员数量
        Integer newPersonnelNumber = userFloatingPopulationRecordMapper.selectYearNewPersonnel(orgId, strYear, residenceCommunityId, communityIds);
        //查询出前端机构月度迁出的人员数量
        Integer emigrantsNumber = userFloatingPopulationRecordMapper.selectYearEmigrantsPersonnel(orgId, strYear, residenceCommunityId, communityIds);
        userFloatingPopulationRecord.setYear(thisYear);

        //如果不存在小区则直接返回
        if (CollectionUtil.isEmpty(communityIds)) {
            userFloatingPopulationRecord.setNewPersonnelNumber(0);
            userFloatingPopulationRecord.setEmigrantsNumber(0);
            return userFloatingPopulationRecord;
        }
        userFloatingPopulationRecord.setNewPersonnelNumber(newPersonnelNumber);
        userFloatingPopulationRecord.setEmigrantsNumber(emigrantsNumber);
        return userFloatingPopulationRecord;
    }

    /**
     * 总人员数据统计
     *
     * @param orgId                组织id
     * @param residenceCommunityId 小区id
     * @param communityIds
     * @return
     */
    @Override
    public UserFloatingPopulationRecord personnelInfo(Long orgId, String residenceCommunityId, List<String> communityIds) {
        UserFloatingPopulationRecord userFloatingPopulationRecord = new UserFloatingPopulationRecord();

        //如果不存在小区则直接返回
        if (CollectionUtil.isEmpty(communityIds)) {
            userFloatingPopulationRecord.setTotalNumber(0);
            return userFloatingPopulationRecord;
        }
        //查询出当前机构总入住人员数量
        Integer newPersonnelInfo = userFloatingPopulationRecordMapper.selectNewPersonnelInfo(orgId, residenceCommunityId, communityIds);
        //查询出当前机构总迁出人员数量
        Integer EmigrantsPersonnelInfo = userFloatingPopulationRecordMapper.selectEmigrantsPersonnelInfo(orgId, residenceCommunityId, communityIds);
        Integer personnelInfo = newPersonnelInfo - EmigrantsPersonnelInfo;
        userFloatingPopulationRecord.setTotalNumber(personnelInfo);
        return userFloatingPopulationRecord;
    }
}
